<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--设备类型数据-->
      <el-col :span="spanColLeft" :xs="24">
        <div class="head-container">
          <el-input
            v-model="drtypename"
            placeholder="请输入设备类型名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deviceTypeOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--设备属性数据-->
      <el-col :span="spanColRight" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="寄存器名" prop="regName">
        <el-input
          v-model="queryParams.regName"
          placeholder="请输入寄存器名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="regUnits">
        <el-input
          v-model="queryParams.regUnits"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
          <el-form-item label="值类型" prop="regType">
            <el-select v-model="queryParams.regType" placeholder="请选择值类型" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_value_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="读写类型" prop="regReadwrite">
            <el-select v-model="queryParams.regReadwrite" placeholder="请选择读写" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_read_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
          <el-form-item label="变量分组" prop="regGroupId">
            <el-select v-model="queryParams.regGroupId" placeholder="变量分组" @change="handleQuery" clearable>
              <el-option
                v-for="group in groupOptions"
                :key="group.id"
                :label="group.groupName"
                :value="group.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="数据回显" prop="regSub">
            <el-select v-model="queryParams.regSub" placeholder="请选择数据回显" @change="handleQuery" clearable>
              <el-option
                v-for="item in subOptions"
                :key="item.subid"
                :label="item.subname"
                :value="String(item.subid)"
              ></el-option>
            </el-select>
          </el-form-item>
      <el-form-item label="点位类型" prop="regDrshowtype">
            <el-select v-model="queryParams.regDrshowtype" placeholder="请选择点位类型" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_reg_dr_show_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
          <el-form-item label="列表显示" prop="islistshow">
            <el-select v-model="queryParams.islistshow" placeholder="请选择列表显示" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_is_show"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="历史存储" prop="ishistory">
            <el-select v-model="queryParams.ishistory" placeholder="请选择是否历史" @change="handleQuery" clearable>
              <el-option
                v-for="dict in dict.type.sys_is_custom_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="是否能耗" prop="isenergy">
            <el-select v-model="queryParams.isenergy" placeholder="请选择是否能耗" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_is_energy"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否报警" prop="isalarm">
            <el-select v-model="queryParams.isalarm" placeholder="请选择是否报警" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_is_alarm"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
          <el-form-item label="采集周期" prop="tagTime">
            <el-select v-model="queryParams.tagTime" placeholder="请选择寄存器采集周期" @change="handleQuery" clearable>
          <el-option
            v-for="dict in dict.type.sys_data_save_time"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
              icon="el-icon-d-arrow-left"
              size="mini"
              @click="handleShowLeft"
            >隐藏/显示左侧选择树</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:drtypemodeglobal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:drtypemodeglobal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:drtypemodeglobal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:drtypemodeglobal:export']"
        >导出</el-button>
      </el-col>
          <span> 全局设备属性变量，影响导入点表绑定</span>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drtypemodeglobalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" prop="typemodeid" width="50"/>
          <el-table-column label="设备类型" align="center" prop="drtypename" />
          <el-table-column label="寄存器名" align="center" prop="regName" width="150"/>
          <el-table-column label="单位" align="center" prop="regUnits" />
          <el-table-column label="排序" align="center" prop="regListshowlevel" />
          <el-table-column label="值类型" align="center" prop="regType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_value_type" :value="scope.row.regType"/>
        </template>
      </el-table-column>
          <el-table-column label="读写类型" align="center" prop="regReadwrite">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_read_type" :value="scope.row.regReadwrite"/>
        </template>
      </el-table-column>
          <!--      <el-table-column label="初始值" align="center" prop="tagValue" />-->
          <el-table-column label="分组" align="center" prop="groupName" />
          <el-table-column label="数据回显" align="center" prop="subName" />
      <el-table-column label="点位类型" align="center" prop="regDrshowtype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_reg_dr_show_type" :value="scope.row.regDrshowtype"/>
        </template>
      </el-table-column>
          <el-table-column label="是否列表显示" align="center" prop="islistshow" width="100">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_is_show" :value="scope.row.islistshow"/>
            </template>
          </el-table-column>
          <!--      <el-table-column label="显示阀值" align="center" prop="regDrsub" />-->
          <el-table-column label="是否历史存储" align="center" prop="ishistory" width="100">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_is_custom_type" :value="scope.row.ishistory"/>
        </template>
      </el-table-column>
      <el-table-column label="是否能耗" align="center" prop="isenergy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_energy" :value="scope.row.isenergy"/>
        </template>
      </el-table-column>
      <el-table-column label="是否报警" align="center" prop="isalarm">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_alarm" :value="scope.row.isalarm"/>
        </template>
      </el-table-column>
          <!--      <el-table-column label="报警类别" align="center" prop="alarmtype" />
                <el-table-column label="报警条件小值" align="center" prop="valueMin" />
                <el-table-column label="报警条件大值" align="center" prop="valueMax" />
                <el-table-column label="报警条件与或" align="center" prop="andOr" />-->
          <el-table-column label="报警级别" align="center" prop="alarmtypeExpain" />
          <el-table-column label="采集周期" align="center" prop="tagTime">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_data_save_time" :value="scope.row.tagTime"/>
        </template>
      </el-table-column>
          <!--      <el-table-column label="颜色" align="center" prop="regcolor" />-->
          <el-table-column label="操作" align="center" class-name="small-padding" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:drtypemodeglobal:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:drtypemodeglobal:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      </el-col>
    </el-row>
    <!-- 添加或修改全局设备类型模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备类型" prop="drtypeid">
              <treeselect v-model="form.drtypeid" :options="drtypeinfoOptions" :normalizer="normalizer" placeholder="请选择设备类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="寄存器名" prop="regName">
          <el-input v-model="form.regName" placeholder="请输入寄存器名" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位" prop="regUnits">
              <el-input v-model="form.regUnits" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="初始值" prop="tagValue">
              <el-input v-model="form.tagValue" placeholder="请输入初始值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" prop="regListshowlevel">
              <el-input v-model="form.regListshowlevel" placeholder="请输入显示顺序" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据回显" prop="regSub" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.regSub" placeholder="请选择数据回显" clearable>
                <el-option
                  v-for="item in subOptions"
                  :key="item.subid"
                  :label="item.subname"
                  :value="String(item.subid)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="读写类型" prop="regReadwrite">
              <el-select v-model="form.regReadwrite" placeholder="请选择读写">
                <el-option
                  v-for="dict in dict.type.sys_read_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="值类型" prop="regType">
          <el-select v-model="form.regType" placeholder="请选择值类型">
            <el-option
              v-for="dict in dict.type.sys_value_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="变量分组" prop="regGroupId" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.regGroupId" placeholder="变量分组" clearable>
                <el-option
                  v-for="group in groupOptions"
                  :key="group.id"
                  :label="group.groupName"
                  :value="group.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="点位类型" prop="regDrshowtype">
          <el-select v-model="form.regDrshowtype" placeholder="请选择点位类型">
            <el-option
              v-for="dict in dict.type.sys_reg_dr_show_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="列表显示" prop="islistshow">
              <el-select v-model="form.islistshow" placeholder="请选择列表显示">
                <el-option
                  v-for="dict in dict.type.sys_is_show"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否历史" prop="ishistory">
              <el-select v-model="form.ishistory" placeholder="请选择是否历史">
                <el-option
                  v-for="dict in dict.type.sys_is_custom_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="是否能耗" prop="isenergy">
          <el-select v-model="form.isenergy" placeholder="请选择是否能耗">
            <el-option
              v-for="dict in dict.type.sys_is_energy"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="是否报警" prop="isalarm">
          <el-select v-model="form.isalarm" placeholder="请选择是否报警">
            <el-option
              v-for="dict in dict.type.sys_is_alarm"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采集周期" prop="tagTime">
          <el-select v-model="form.tagTime" placeholder="请选择寄存器采集周期">
            <el-option
              v-for="dict in dict.type.sys_data_save_time"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警等级" prop="alarmLevel" style="padding-left: 12px" label-width="90">
              <el-select v-model="form.alarmLevel" placeholder="报警等级(仅布尔变量)" clearable>
                <el-option
                  v-for="alarmLevel in alarmLevelOptions"
                  :key="alarmLevel.alarmtypelevel"
                  :label="alarmLevel.alarmtypeExpain"
                  :value="alarmLevel.alarmtypelevel"
                ></el-option>
              </el-select>(仅布尔变量的值类型)
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDrtypemodeglobal, getDrtypemodeglobal, delDrtypemodeglobal, addDrtypemodeglobal, updateDrtypemodeglobal,groupSelect, drTypeSelect, deviceTypeTreeSelect,getSubOptionSelect } from "@/api/system/drtypemodeglobal";
import {alarmLevelSelect} from "@/api/system/qstag";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Drtypemodeglobal",
  components: {
    Treeselect
  },
  dicts: ['sys_reg_dr_show_type', 'sys_is_custom_type', 'sys_is_show', 'sys_is_energy', 'sys_read_type', 'sys_yes_no', 'sys_data_save_time', 'sys_is_alarm', 'sys_value_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      showLeft: true,
      spanColLeft: 4,
      spanColRight: 20,
      // 总条数
      total: 0,
      // 全局设备类型模板表格数据
      drtypemodeglobalList: [],
      //报警等级
      alarmLevelOptions: undefined,
      // 设备类型管理树选项
      drtypeinfoOptions: [],
      // 设备类型选项
      deviceTypeOptions: undefined,
      //变量分组
      groupOptions: undefined,
      // 设备属性回显定义列表
      subOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 设备类型名称
      drtypename: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drtypeid: null,
        regName: null,
        regType: null,
        regUnits: null,
        regReadwrite: null,
        regGroupId: null,
        regSub: null,
        regDrshowtype: null,
        regDrsub: null,
        ishistory: null,
        isenergy: null,
        isalarm: null,
        alarmLevel: null,
        islistshow: null,
        tagTime: null,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    drtypename(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getAlarmLevelSelect();
    this.getGroupSelect();
    this.getDrTypeSelect();
    this.getDeviceTypeTree();
    this.getSubList();
  },
  methods: {
    /** 查询全局设备类型模板列表 */
    getList() {
      this.loading = true;
      listDrtypemodeglobal(this.queryParams).then(response => {
        this.drtypemodeglobalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询数据回显列表 */
    getSubList() {
      getSubOptionSelect().then(response => {
        this.subOptions = response.data;
      });
    },
    handleShowLeft(){
      const show = this.showLeft;
      if(show){
        this.showLeft = false;
        this.spanColLeft = 0;
        this.spanColRight = 24;
      }else{
        this.showLeft = true;
        this.spanColLeft = 4;
        this.spanColRight = 20;
      }
    },
    /** 转换设备类型管理数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.drtypeid,
        label: node.drtypename,
        children: node.children
      };
    },
    /** 查询设备类型管理下拉树结构 */
    getDrTypeSelect() {
      drTypeSelect().then(response => {
        this.drtypeinfoOptions = [];
        const data = { drtypeid: 0, drtypename: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "drtypeid", "parentId");
        this.drtypeinfoOptions.push(data);
      });
    },
    /** 查询设备类型下拉树结构 */
    getDeviceTypeTree() {
      deviceTypeTreeSelect().then(response => {
        this.deviceTypeOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.drtypeid = data.id;
      this.handleQuery();
    },
    /** 查询报警等级类型 */
    getAlarmLevelSelect() {
      alarmLevelSelect().then(response => {
        this.alarmLevelOptions = response.data;
      });
    },
    /** 查询变量分组 */
    getGroupSelect() {
      groupSelect().then(response => {
        this.groupOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        typemodeid: null,
        drtypeid: null,
        regName: null,
        regType: null,
        regUnits: null,
        regReadwrite: null,
        tagValue: null,
        regGroupId: null,
        regListshowlevel: null,
        regSub: null,
        regDrshowtype: null,
        regDrsub: null,
        ishistory: null,
        isenergy: null,
        isalarm: null,
        alarmtype: null,
        valueMin: null,
        valueMax: null,
        andOr: null,
        alarmLevel: null,
        islistshow: null,
        tagTime: null,
        regcolor: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.drtypeid = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.typemodeid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.drtypeid = 0;
      this.open = true;
      this.title = "添加全局设备类型模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const typemodeid = row.typemodeid || this.ids
      this.form.parentId = row.parentId;
      getDrtypemodeglobal(typemodeid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改全局设备类型模板";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.typemodeid != null) {
            updateDrtypemodeglobal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrtypemodeglobal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const typemodeids = row.typemodeid || this.ids;
      this.$modal.confirm('是否确认删除全局设备类型模板编号为"' + typemodeids + '"的数据项？').then(function() {
        return delDrtypemodeglobal(typemodeids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/drtypemodeglobal/export', {
        ...this.queryParams
      }, `drtypemodeglobal_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
